﻿package com.codingbros.fireworks.particles 
{
	
	import flash.display.Shape;
	
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author ...
	 */
	public class TechLineParticle extends Shape implements ILineParticle
	{

		private static const MAXSPEED:Number=8;
		private static const MINSPEED:Number=4;

		private var tick:int=0;

		private var currentPos:Point;
		private var lastPos:Point;

		private var velocity:Point;
		private var acceleration:Point;
		private var gravity:Point;

		private var sideWind:Point;

		private var color:uint;
		private var lineThickness:Number=0;

		public function TechLineParticle() 
		{
			//currentPos=new Point();
			//fuck motion:
			currentPos=new Point(-40+Math.random()*80,-40+Math.random()*80);
			//currentPos=new Point(-3 + Math.random()*6, -4 + Math.random()*8);

			lastPos=new Point();

			var radians:Number=Math.random()*Math.PI*2;
			var speed:Number = MINSPEED + Math.random() * (MAXSPEED - MINSPEED);

			velocity = new Point();
			velocity.x=Math.cos(radians)*speed;
			velocity.y=Math.sin(radians)*speed;

			acceleration = new Point();
			gravity = new Point();
		}
		
		public function init(theColor:uint):void 
		{
			color=theColor;
		}

		public function update():void 
		{
			lastPos=currentPos.clone();

			gravity=currentPos.clone();
			gravity.normalize(-0.12);

			acceleration=gravity;

			velocity=velocity.add(acceleration);

			//fuck motion
			//velocity=velocity.add(new Point(-0.04+Math.random()*0.08,-0.04+Math.random()*0.08));
			velocity.x += -0.04+Math.random()*0.08;
			velocity.y += -0.04+Math.random()*0.08; 

			currentPos=currentPos.add(velocity);

			lineThickness = 2 + velocity.length / 2;
	
			tick++;
		}	

		public function draw():void 
		{
			graphics.clear();
			
			if (tick%2 == 0) {
				graphics.lineStyle(0.5,color,1);
				graphics.lineTo(currentPos.x, currentPos.y);
			}
			
			graphics.lineStyle(3,color,1);
			graphics.moveTo(lastPos.x, lastPos.y);
			graphics.lineTo(currentPos.x, currentPos.y);
		}
	}	
}